package com.jiawa.wiki.controller;

import com.jiawa.wiki.req.EbookQueryReq;
import com.jiawa.wiki.req.EbookSaveReq;
import com.jiawa.wiki.response.CommonResp;
import com.jiawa.wiki.response.EbookResp;
import com.jiawa.wiki.response.PageResp;
import com.jiawa.wiki.service.EbookService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 参数校验的时候需要 在此加@Valid注解，才能扫描到请求实体中的校验参数
 */
@RestController
@RequestMapping("/ebook")
public class EbookController {

    @Resource
    private EbookService ebookService;

    /**
     * 查询电子书列表
     * @param req
     * @return
     */
    @GetMapping("/list")
    public CommonResp<PageResp<EbookResp>> list(@Valid EbookQueryReq req) {
        // 实现模糊查询
        CommonResp<PageResp<EbookResp>> resp = new CommonResp();
        PageResp<EbookResp> ebooks = ebookService.list(req);
        resp.setContent(ebooks);
        return resp;
    }

    /**
     * 保存修改电子书
     * @param req
     * @return
     */
    @PostMapping("/save")
    public CommonResp save(@Valid @RequestBody EbookSaveReq req) {
        CommonResp resp = new CommonResp<>();
        ebookService.save(req);
        return resp;
    }

    /**
     * 根据id删除电子书
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public CommonResp delete(@PathVariable Long id) {
        CommonResp resp = new CommonResp<>();
        ebookService.delete(id);
        return resp;
    }
}
